import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { environment } from '@env/environment';
import { STChange, STClickRowClassNameType, STColumn, STComponent, STPage } from '@delon/abc/st';
import { FormBuilder } from '@angular/forms';
import { CargoStorageService } from '../../cargo-storage/cargo-storage.service';
import { deepCopy } from '@delon/util';
import { RetreatStatService } from '../retreat-stat.service';
import { TstOriToDetailComponent } from '../../../ship-stat/tst-ori-to-detail/tst-ori-to-detail.component';
import { DragModalService } from '../../../../../shared/utils/drag-modal.service';

@Component({
  selector: 'app-detail',
  templateUrl: './detail.component.html',
  styleUrls: ['./detail.component.less']
})
export class RetreatStatDetailComponent implements OnInit {
	@Input()
	shipInfo: any;

	selectedStorage: any;

	retreatSetPageInfo = {
		total: 0,
		current: 1,
		size: 20,
		loading: false,
		nzPageSizeOptions: environment.nzPageSizeOptions
	};
	retreatSetPageSetting: STPage = {
		total: '',
		show: true,
		showSize: true,
		front: false,
		pageSizes: environment.nzPageSizeOptions
	};

	tallyPageInfo = {
		total: 0,
		current: 1,
		size: 20,
		loading: false,
		nzPageSizeOptions: environment.nzPageSizeOptions
	};
	tallyPageSetting: STPage = {
		total: '',
		show: true,
		showSize: true,
		front: false,
		pageSizes: environment.nzPageSizeOptions
	};

	@ViewChild('st1', { static: false }) private storageSt!: STComponent;
	retreatSetColumns: STColumn[] = [
		{
			title: '序号',
			width: 50,
			index: 'index',
			type: 'no',
			noIndex: (item, col, index) => index + 1 + (this.storageSt.pi - 1) * this.storageSt.ps,
			fixed: 'left',
			className: 'tc'
		},
		{
			title: '货场货位',
			index: 'positionInfo',
			width: 80
		},
		{
			title: '件吨',
			index: 'retreatPieceTonInfo',
			width: 100
		},
		{
			title: '姓名',
			index: 'createUserName',
			width: 60
		},
		{
			title: '日期班次',
			index: 'timeInfo',
			width: 80
		},
		{
			title: '退关原因',
			index: 'retreatTypeName',
			width: 80
		},
		{
			title: '提货',
			index: 'outPieceTonInfo',
			width: 100
		},
		{
			title: '结余',
			index: 'leftPieceTonInfo',
			width: 100
		}
	];
	retreatSetDataList = [];

	@ViewChild('st2', { static: false }) private tallySt!: STComponent;
	tallyColumns: STColumn[] = [
		{
			title: '序号',
			width: 50,
			index: 'index',
			type: 'no',
			noIndex: (item, col, index) => index + 1 + (this.tallySt.pi - 1) * this.tallySt.ps,
			fixed: 'left',
			className: 'tc'
		},
		{
			title: '提货日期',
			index: 'timeInfo',
			width: 120
		},
		{
			title: '提货方式',
			index: 'tallyType',
			render: 'tallyType',
			width: 120
		},
		{
			title: '件吨',
			index: 'tallyPieceTonInfo',
			width: 120
		},
		{
			title: '理货员',
			index: 'createUserName',
			width: 120
		}
	];
	tallyDataList = [];
	constructor(
		private fb: FormBuilder,
		private retreatStatService: RetreatStatService,
		private modalService: DragModalService
	) {
	}

	ngOnInit(): void {
		this.loadRetreatSetData();
	}

	loadRetreatSetData(): any {
		this.retreatSetPageInfo.loading = true;
		const params = {
			transportBillId: this.shipInfo?.transportBillId,
			...this.retreatSetPageInfo,
		};
		this.retreatStatService.retreatSetStatDetailPage(params).subscribe((res: any) => {
			this.retreatSetDataList = res.records;
			this.retreatSetPageInfo.total = res.total;
			this.retreatSetPageSetting.total = res.total;
			this.retreatSetPageInfo.loading = false;

			if (this.retreatSetDataList.length > 0) {
				this.selectedStorage = this.retreatSetDataList[0];
				this.tallyPageInfo.current = 1;
				this.loadTallyData();
			}
		});
	}

	loadTallyData(): any {
		this.tallyPageInfo.loading = true;
		const params = {
			retreatStorageId: this.selectedStorage.retreatStorageId,
			...this.tallyPageInfo,
		};
		this.retreatStatService.retreatSetStatTallyPage(params).subscribe((res: any) => {
			this.tallyDataList = res.records;
			this.tallyPageInfo.total = res.total;
			this.tallyPageSetting.total = res.total;
			this.tallyPageInfo.loading = false;
		});
	}


	storageStChange(ret: STChange) {
		if (ret.type === 'click') {
			if (ret.click.item) {
				this.selectedStorage = deepCopy(ret.click.item);
				this.tallyPageInfo.current = 1;
				this.loadTallyData();
			}
		}
		if (ret.type === 'pi' || ret.type === 'ps') {
			this.retreatSetPageInfo.current = ret.pi;
			this.retreatSetPageInfo.size = ret.ps;
			this.loadRetreatSetData();
		}
	}

	tallyStChange(ret: STChange) {
		if (ret.type === 'pi' || ret.type === 'ps') {
			this.tallyPageInfo.current = ret.pi;
			this.tallyPageInfo.size = ret.ps;
			this.loadTallyData();
		}
	}

	sa: STClickRowClassNameType = {
		fn(record: any, index: number): string {
			return 'selected-row';
		},
		exclusive: true
	};

	showTstDetail(item: any) {
		this.modalService
			.createDrag({
				nzContent: TstOriToDetailComponent,
				nzComponentParams: { tallyId: item.tallyId },
				nzTitle: '详情',
				nzFooter: null
			})
			.afterClose.subscribe(res => {
		});
	}
}
